package com.sunday.fangdaifu.activity.task;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.adapter.HistoryDetailAdapter;
import com.sunday.fangdaifu.common.ApiClient;
import com.sunday.fangdaifu.common.ApiServiceImpl;
import com.sunday.fangdaifu.model.HistoryTaskDetail;
import com.sunday.fangdaifu.model.HistoryTaskList;
import com.sunday.fangdaifu.widgets.RecordTimer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yun_sheng on 2017/7/5.
 */

public class HistoryTaskDetailActivity extends BaseActivity implements ApiServiceImpl.HttpResponseInterface {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.title_bar)
    RelativeLayout titleBar;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    @Bind(R.id.btn_play_stop)
    TextView btnPlayStop;
    @Bind(R.id.btn_start_play)
    ImageView btnStartPlay;
    @Bind(R.id.progress_bar)
    ProgressBar progressBar;
    @Bind(R.id.timer_start)
    TextView timer_Start;
    @Bind(R.id.timer_end)
    TextView timer_End;
    @Bind(R.id.layout_play_recorder)
    RelativeLayout layoutPlayRecorder;

    private HistoryTaskList historyTaskList;
    private HistoryDetailAdapter adapter;
    private LinearLayoutManager layoutManager;
    private List<HistoryTaskDetail> details = new ArrayList<>();
    private MediaPlayer mediaPlayer;
    HistoryTaskDetail detail;
    RecordTimer timerStart;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        historyTaskList = (HistoryTaskList) getIntent().getSerializableExtra("historyTaskList");
        init();
    }

    private void init() {
        title.setText(historyTaskList.getRemark());
        adapter = new HistoryDetailAdapter(mContext);
        layoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        showLoadingDialog();
        ApiClient.getApiService().historyReportDetail(historyTaskList.getId(), this, new TypeToken<ResultDO<List<HistoryTaskDetail>>>() {
        }.getType());


        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.layout_play_record:
                        detail = (HistoryTaskDetail) v.getTag();
                        layoutPlayRecorder.setVisibility(View.VISIBLE);
                        play();
                        break;
                }

            }
        });
        timer_Start.setVisibility(View.GONE);

    }

    private void play() {
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(detail.getVideo());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        timerStart = new RecordTimer(999999999, 1000, timer_Start);
        timerStart.start();
        btnStartPlay.setImageResource(R.mipmap.report_voice_stop);

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                btnStartPlay.setImageResource(R.mipmap.report_voice_play);
                if (timerStart != null)
                    timerStart.cancel();
            }
        });
    }

    @OnClick({R.id.btn_start_play, R.id.btn_play_stop})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_start_play:
                if (mediaPlayer != null) {
                    if (mediaPlayer.isPlaying()) {
                        replay();
                    } else {
                        pause();
                    }
                }
                break;
            case R.id.btn_play_stop:
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.stop();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
                if (timerStart!=null){
                    timerStart.cancel();
                }
                layoutPlayRecorder.setVisibility(View.GONE);
                break;
        }
    }

    private void replay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            btnStartPlay.setImageResource(R.mipmap.report_voice_stop);
            if (timerStart != null) {
                timerStart.cancel();
            }
            timerStart = new RecordTimer(999999999, 1000, timer_Start);
            timerStart.start();
            return;
        }
        play();
    }

    private void pause() {

    }


    @Override
    public void onResponce(String key, Object result) {
        if (isFinish) {
            return;
        }
        dissMissDialog();
        ResultDO<List<HistoryTaskDetail>> resultDO = (ResultDO<List<HistoryTaskDetail>>) result;
        if (resultDO.getCode() == 0) {
            List<HistoryTaskDetail> historyTaskLists = resultDO.getResult();
            if (details != null)
                details.clear();
            details.addAll(historyTaskLists);
            adapter.setData(details);
            adapter.notifyDataSetChanged();
        } else {
            ToastUtils.showToast(mContext, resultDO.getMessage() + "");
        }
    }

    @Override
    public void onFailure() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
        if (timerStart != null) {
            timerStart.cancel();
        }
    }
}
